$(document).ready(function () {
	$("#forget-password").click(function() {
        $(".login-form").hide(),
        $(".forget-form").show()
    });
    $("#back-btn").click(function() {
        $(".login-form").show(),
        $(".forget-form").hide()
    });
	function Get_Captcha(fresh)
	{
		$.post('/welcome/get_captcha',{},function(data){
			$("#sCap").html(data);
			if(fresh)
			{
				// $("#txtCode").valid();
			}
		});
	}
	Get_Captcha(false);
	$(".aClear").click(function(){
		Get_Captcha(true);
	});

	var icon = {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    };

	$("form[name='loginForm']").bootstrapValidator({
        feedbackIcons: icon,
        live: 'enabled', 
        fields: {
            txtUsername: {
                validators: {
                    notEmpty: {message: '请输入用户名'}
                }
            },
            txtPasswd : {
                validators : {
                    notEmpty : {message : "请输入密码"}
                }
            },
            txtCode: {
                validators: {
                   notEmpty: {message: '请输入验证码'},
                   stringLength: { 
                        min: 4,
                        max: 4,
                        message: '验证码必须是4位'
                    },
                    remote: { 
                          url: '/welcome/checkcaptcha',  
                          message: '验证码不正确', 
                          type: 'POST'
                    }
                }
            }
        }
    }).on("success.form.bv",function(e) {
    	// $('#md5Password').val(md5($('#txtPasswd').val()));
    	$('#md5Password').val(($('#txtPasswd').val()));
		$('#txtPasswd').attr('name','');
		//提交数据
        $.ajax({
            url: '/welcome/dologin',
            type:'post',
            dataType:'json',
            data:
                {
                    txtUsername:$('#txtUsername').val(),
                    md5Password:$('#md5Password').val()
                },
            beforeSend: function(){
                $('.submit-sure').text('提交中...');
            },
            success:function(r){
                if (r.code == 0) {
                    window.location.href= '/admin';
                }else {
                    alert('用户名或密码输入不正确！');
                }
            }
        });
        return false;
    });
	
    $("#register-btn").click(function() {
        $(".login-form").hide();
        $(".register-form").show();

    }),
    $("#register-back-btn").click(function() {
        $(".login-form").show();
        $(".register-form").hide();
    });


    $("form[name='registerForm']").bootstrapValidator({
        feedbackIcons: icon,
        live: 'enabled', 
        //表单域配置
        fields: {
            regUsername: {
                validators: {
                    notEmpty: {message: '请输入用户名'},
                    stringLength: { 
                        min: 2,
                        max: 30,
                        message: '用户名长度必须在2到30之间'
                    },
                    remote: { 
                          url: '/welcome/checkaccount',  
                          message: '用户名已存在', 
                          type: 'POST'
                    }
                }
            },
            regPasswd: {
                validators: {
                   notEmpty: {message: '请输入密码'},
                    regexp: {
                          regexp: /(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{8,16}/,
                          message:'密码长度大于8小于16，且包括大小写字母、数字'
                    }
                }
            },
            regPasswdConfirm: {
                validators: {
                    notEmpty: {message: '请再次输入密码'},
                    identical: {  
                           field: 'regPasswd',
                           message: '两次密码不一致'
                    }
                }
            },
            regMobile : {
                validators : {
                    notEmpty : {
                        message : "请输入电话号码"
                    },
                    regexp: {
                          regexp: /(^(\d{3,4}-)?\d{6,8}$)|(^(\d{3,4}-)?\d{6,8}(-\d{1,5})?$)|^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/,
                          message:'请输入正确的电话号码'
                    },
                    remote: { 
                          url: '/welcome/checkphone',  
                          message: '电话号码已存在', 
                          type: 'POST',
                          data : {
                                username: function() {
                                    return $("#regUsername").val();
                                },
                                txtMobile:function() {
                                    return $("#regMobile").val();
                                }
                          }             
                    }
                }
            }
        }
    
    }).on("success.form.bv",function(e) {
    	$('#regmd5Password').val(md5($('#regPasswd').val()));
		$('#regPasswd').val('');
		$('#regPasswdConfirm').val('');
    });

});

